#include "FightScreen.h"
#include "Character\PlayerCharacter.h"
#include "Character\NonPlayerCharacter.h"
#include "EnemyFightInfo.h"
#include "CharacterFightInfo.h"
#include "qpushbutton.h"
#include "qmessagebox.h"
#include "qlabel.h"
#include "qpainter.h"
#include "qfile.h"
#include <vector>

FightScreen::FightScreen(PlayerCharacter &iPlayer, NonPlayerCharacter &iEnemy, QWidget * parent) 
	: QDialog(parent)
	, mPlayer(iPlayer)
	, mEnemy(iEnemy)
	, mFightOutcome(Outcome::eEvaded)
	, layout(0)
	, mPlayerInfo(0)
	, mEnemyInfo(0)
	, buttonsLayout(0)
	, evadeButton(0)
	, fightButton(0)
	, buttonsPanel(0)
	, rerollButton(0)
	, playerDie(0)
	, enemyDie(0)
	, dieLayout(0)
	, dieWidget(0)
	, result(0)
	, okButton(0)
	, spellButton(0)
{
	layout = new QHBoxLayout;
	result = new QLabel("Choose Action!");
	result->setAlignment(Qt::AlignCenter);
	mPlayerInfo = new CharacterFightInfo(mPlayer);
	mEnemyInfo = new EnemyFightInfo(mEnemy);

	layout->addWidget(mPlayerInfo);

	buttonsLayout = new QVBoxLayout();
	fightButton = new QPushButton("Fight!");
	evadeButton = new QPushButton("Evade");
	rerollButton  = new QPushButton("Reroll!");
	okButton  = new QPushButton("END FIGHT");
	spellButton = new QPushButton("Cast Spell...");
	if (mPlayer.isToad())
		spellButton->setEnabled(false);

	connect(evadeButton, SIGNAL(clicked()), this, SLOT(evade()));
	connect(fightButton, SIGNAL(clicked()), this, SLOT(fight()));
	connect(rerollButton, SIGNAL(clicked()), this, SLOT(reroll()));
	connect(okButton, SIGNAL(clicked()), this, SLOT(ok()));
	connect(spellButton, SIGNAL(clicked()), this, SLOT(spell()));

	rerollButton->setEnabled(false);
	playerDie = new Die;
	enemyDie = new Die;
	playerDie->forceValue(1);
	enemyDie->forceValue(1);

	buttonsLayout->addWidget(fightButton);
	buttonsLayout->addWidget(evadeButton);
	buttonsLayout->addWidget(rerollButton);
	buttonsLayout->addWidget(spellButton);
	
	dieLayout = new QHBoxLayout;
	dieWidget = new QWidget;
	dieLayout->addWidget(playerDie);
	dieLayout->addWidget(enemyDie);
	dieWidget->setLayout(dieLayout);
	buttonsLayout->addWidget(dieWidget);
	buttonsLayout->addWidget(result);
	buttonsLayout->addWidget(okButton);

	buttonsPanel = new QWidget;
	buttonsPanel->setLayout(buttonsLayout);
	layout->addWidget(buttonsPanel);

	layout->addWidget(mEnemyInfo);
	
	this->setLayout(layout);

	setFixedSize(800, 300);
}

void FightScreen::evade(void)
{
	mFightOutcome=Outcome::eEvaded;
	this->close();
}

void FightScreen::ok(void)
{
	this->close();
}

void FightScreen::spell(void)
{
	std::string spells;
	QMessageBox msgBox;
	QString msg = "";

	if (mPlayer.getSpells().size() > 0)
		msg.append("YOU HAVE THE FOLLOWING SPELLS: \n\n");
	else
		msg.append("You have no spell cards.");
	
	for (vector<ISpell *>::size_type i = 0; i < mPlayer.getSpells().size(); ++i)
	{
		msg.append("-");
		msg.append(QString(mPlayer.getSpells().at(i)->getName().c_str()));
		msg.append("\n");
	}
	msgBox.setText(msg);
	msgBox.setIcon(QMessageBox::Information);
	msgBox.exec();

	for (vector<ISpell *>::iterator i = mPlayer.getSpells().begin(); i != mPlayer.getSpells().end(); ++i)
	{
		QMessageBox::StandardButton response;
		response = QMessageBox::question ( this, tr("Spell cast"), tr(std::string("Use this spell? ").append((*i)->getName()).c_str()), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
		if (response == QMessageBox::Yes)
		{
			(*i)->cast(mPlayer);
			i = mPlayer.getSpells().erase(i);
			break;
		}
	}
}

void FightScreen::reroll(void)
{
	if (mPlayer.getFateTokenNumberInPossession() > 0)
	{
		int oldFate = mPlayer.getFateTokenNumberInPossession();
		mPlayer.setFateTokenNumberInPossession(oldFate-1);
		fight();
		rerollButton->setEnabled(false);
	}
	else
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("You do not have any fate tokens left!"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		rerollButton->setEnabled(false);
	}
}

void FightScreen::fight(void)
{
	evadeButton->setEnabled(false);
	playerDie->roll();
	enemyDie->roll();
	this->update();
	int playerScore = playerDie->read() + mPlayer.getTotalCharacterStrength();
	int enemyScore = enemyDie->read() + mEnemy.getStrengthValue();
	if (playerScore > enemyScore)
	{
		result->setText("You win!");
		fightButton->setEnabled(false);
		rerollButton->setEnabled(false);
		spellButton->setEnabled(false);
		mFightOutcome = Outcome::eWinFight;
	}
	else if (playerScore < enemyScore)
	{
		result->setText("You lose!");
		fightButton->setEnabled(false);
		rerollButton->setEnabled(true);
		mFightOutcome = Outcome::eLoseFight;
	}
	else
	{
		result->setText("Standoff !");
		fightButton->setEnabled(false);
		rerollButton->setEnabled(true);
		mFightOutcome = Outcome::eStandoff;
	}
}

FightScreen::~FightScreen()
{
	if(0 != mPlayerInfo)
		delete mPlayerInfo;
	if (0 != mEnemyInfo)
		delete mEnemyInfo;
	if (0 != evadeButton)
		delete evadeButton;
	if (0 != rerollButton)
		delete rerollButton;
	if (0 != fightButton)
		delete fightButton;
	if (0 != spellButton)
		delete spellButton;
	if (0 != okButton)
		delete okButton;
	if (0 != playerDie)
		delete playerDie;
	if (0 != enemyDie)
		delete enemyDie;
	if (0 != dieLayout)
		delete dieLayout;
	if (0 != dieWidget)
		delete dieWidget;
	if (0 !=buttonsLayout)
		delete buttonsLayout;
	if (0 != buttonsPanel)
		delete buttonsPanel;
	if (0 != layout)
		delete layout;
}